package com.jdl.iscm.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.google.common.collect.Lists;
import com.jdl.iscm.entity.excel.RqLlamaExcel;
import org.testng.collections.Maps;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @Author: Ran
 * @Date: 2021/8/23
 **/
public class RqLlamaListener extends AnalysisEventListener<RqLlamaExcel> {

    private final List<RqLlamaExcel> rqLlamaExcelList = Lists.newArrayList();

    @Override
    public void invoke(RqLlamaExcel rqLlamaExcel, AnalysisContext analysisContext) {
        rqLlamaExcelList.add(rqLlamaExcel);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        rqLlamaExcelList.stream().map(rqLlamaExcel -> {
            //用于设置维度 & 场景
            Map<String, String> map = Maps.newLinkedHashMap();
            List<String> list = Lists.newArrayList();
            String[] strArr1 = rqLlamaExcel.getDimensions().split(",");
            IntStream.range(0, strArr1.length).forEach(x -> {
                String[] dimension = strArr1[x].split("=");
                map.put(dimension[0], dimension[1]);
            });
            String[] strArr2 = rqLlamaExcel.getScenes().split(",");
            IntStream.range(0, strArr2.length).forEach(x -> list.add(strArr2[x]));
            rqLlamaExcel.setDimensionMap(map);
            rqLlamaExcel.setSceneList(list);
            return rqLlamaExcel;
        }).collect(Collectors.toList());
    }
}
